let mongoose = require('mongoose');

let schema = new mongoose.Schema({
    sysMsg : [require('./gameUser.message.item')],
    email : [require('./gameUser.message.item')],
    noticeStatus : {
        type: [require('./NoticeStatus')],
        default : []
    }
});

schema.methods = {
    getMinList(msgs){
        let obj = [];
        for(let i=0; i<msgs.length; i++){
            let msg = msgs[i]
            let o = {
                _id:msg._id.toString(),
                title : String,
                time :msg.time,
                from : msg.from,
                overdueTime : msg.overdueTime,
                content : msg.content,
                images : msg.images,
                props : msg.props,
                status : msg.status,
                scene : msg.scene
            }
            obj.push(o)
        }
        return obj
    },
    async sysList(){
        return this.sysMsg
    },
    async emailList(){
        return this.email
    },
    async newNotice(self){
        let NoticeModel = mongoose.models['Notice']
        let time = Date.now()
        let notices = await NoticeModel.find({});
        let status = [];
        let temp = [];
        let i=0;
        while(i<notices.length){
            if(notices[i].overdueTime < time){
                await NoticeModel.remove({_id:notices[i]._id})
                notices.splice(i,1);
            } else {
                let ad =this.noticeStatus.find(item=>{return item.id == notices[i]._id})
                if(!ad){
                    temp.push(notices[i]);
                    status.push({id:notices[i].id,status:1})
                }
                i++
            }
        }

        this.noticeStatus = status;
        await self.save();
        return temp
    },
    async getNotice(options){
        let NoticeModel = mongoose.models['Notice']
        let noticesCfg = await NoticeModel.findOne({_id:options.id});
        console.log('this.noticeStatus:',this.noticeStatus,noticesCfg.props)
        let res = [];
        for(let i=0; i<this.noticeStatus.length; i++){
            let item = this.noticeStatus[i]
            if(item.id == options.id){
                if(item.status == 2){
                    throw new Error('已经领取过该奖励')
                }
                item.status = 1;
                if(noticesCfg.props.length >=1){
                    res.push({type:1,count:noticesCfg.props[0]})
                    options.self.store.diamond += parseInt(noticesCfg.props[0])
                }
                if(noticesCfg.props.length >=2) {
                    res.push({type:1,count:noticesCfg.props[1]})
                    options.self.store.goldCoin += parseInt(noticesCfg.props[1])
                }
                await options.self.save()
                break;
            }
        }
        return res
    },
    async read(options){
        let msg = null;
        if(options.type == 1){
            msg = this.email.find(item=>{return item.id==options.id})
        } else {
            msg = this.sysMsg.find(item=>{return item.id==options.id})
        }
        if(!msg){
            throw new Error('not found msg info');
        }
        msg.status = 1;
        await options.self.save()
        return msg;
    },
    async getProp(options){
        let msg = null;
        if(options.type == 1){
            msg = this.email.find(item=>{return item.id==options.id})
        } else {
            msg = this.sysMsg.find(item=>{return item.id==options.id})
        }
        if(!msg){
            throw new Error('not found msg info');
        }
        if(msg.status == 2){
            throw new Error('已经领取过奖励');
        }
        let res =[];
        if(msg.props.length >=1){
            res.push({type:1,count:msg.props[0]})
            options.self.store.diamond += parseInt(msg.props[0])
        }
        if(msg.props.length >=2) {
            res.push({type:2,count:msg.props[1]})
            options.self.store.goldCoin += parseInt(msg.props[1])
        }
        if(options.type != 1){
            let idx = this.sysMsg.findIndex(item=>{return item.id==options.id})
            this.sysMsg.splice(idx,1);
        } else {
            msg.status = 2
        }

        await options.self.save()
        return res;
    },
}

module.exports = schema